{% extends 'layout/base.html' %}

{% block content %}
<div class="row mt-5">
    <div class="col-lg"></div>
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">{{ 'Edit' if dns_zone_id > 0 else 'Create' }} DNS Zone</div>
            <div class="card-body">
                <form action="{{ url_for('dns.zone_edit_save', dns_zone_id=dns_zone_id) }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

                    <div class="form-group">
                        <label for="domain">Domain</label>
                        {% if zone.master == True %}
                        <input type="text" name="domain" id="domain" class="form-control" value="{{ zone.domain|replace('.' + user_domain, '') if dns_zone_id > 0 }}" placeholder="any.domain.you.want.com" required readonly>
                        <div class="text-right font-italic"><small>This is a master zone - you can only edit the below settings.</small></div>
                        {% else %}
                        <div class="input-group">
                            {% if current_user.admin == True %}
                            <input type="text" name="domain" id="domain" class="form-control" value="{{ zone.domain if dns_zone_id > 0 }}" placeholder="any.domain.you.want.com" required autofocus>
                            {% else %}
                            <input type="text" name="domain" id="domain" class="form-control" value="{{ zone.domain|replace('.' + user_domain, '') if dns_zone_id > 0 }}" placeholder="any.domain.you.want.com" required autofocus>
                            <div class="input-group-append">
                                <span class="input-group-text">.{{ user_domain }}</span>
                            </div>
                            {% endif %}
                        </div>
                        {% endif %}
                    </div>

                    <div class="form-group row py-0 my-0">
                        <div class="col">
                            <label for="active">Active</label>
                        </div>
                        <div class="col text-right">
                            <div class="custom-control custom-switch">
                                <input name="active" type="checkbox" class="custom-control-input" id="active" value="1" {{ 'checked' if (dns_zone_id == 0) or (dns_zone_id > 0 and zone.active) }}>
                                <label class="custom-control-label" for="active"></label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group row py-0 my-0">
                        <div class="col">
                            <label for="catch_all">
                                Catch-All
                                <a href="#" title="" data-container="body" data-toggle="popover" data-placement="right" data-content="All subdomains for this zone will match this domain, if no other match is found. If disabled, the incoming DNS must match the full domain in order to get a response. Fallback matches will be ignored.<br><br>This setting is ignored when Regular Expressions are enabled." data-original-title="Catch-All"><i class="fas fa-question-circle ml-1"></i></a>
                            </label>
                        </div>
                        <div class="col text-right">
                            <div class="custom-control custom-switch">
                                <input name="catch_all" type="checkbox" class="custom-control-input" id="catch_all" value="1" {{ 'checked' if zone.catch_all }}>
                                <label class="custom-control-label" for="catch_all"></label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group row py-0 my-0">
                        <div class="col">
                            <label for="forwarding" class="text-nowrap">
                                Unmatched Record Forwarding
                                <a href="#" title="" data-container="body" data-toggle="popover" data-placement="right" data-content="Example: If Record A is matched but Record AAAA isn't, determine whether to forward the AAAA request to an external DNS Server (if DNS Forwarding is enabled)" data-original-title="Catch-All"><i class="fas fa-question-circle ml-1"></i></a>
                            </label>
                        </div>
                        <div class="col text-right">
                            <div class="custom-control custom-switch">
                                <input name="forwarding" type="checkbox" class="custom-control-input" id="forwarding" value="1" {{ 'checked' if (dns_zone_id > 0 and zone.forwarding) }}>
                                <label class="custom-control-label" for="forwarding"></label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group row py-0 my-0">
                        <div class="col">
                            <label for="regex" class="text-nowrap">
                                Regular Expression
                                <a href="#" title="" data-container="body" data-toggle="popover" data-placement="right" data-content="The value of the domain will be evaluated as a regular expression. Make sure that no two zones share a regular expression where a domain can evaluate to true in both.<br><br>To match:<br><span class='text-monospace'>hello###.example.com</span><br>Enter:<br><span class='text-monospace'>hello[0-9]{3}\.example\.com</span>" data-original-title="Regular Expression"><i class="fas fa-question-circle ml-1"></i></a>
                            </label>
                        </div>
                        <div class="col text-right">
                            <div class="custom-control custom-switch">
                                <input name="regex" type="checkbox" class="custom-control-input" id="regex" value="1" {{ 'checked' if (dns_zone_id > 0 and zone.regex) }}>
                                <label class="custom-control-label" for="regex"></label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="tags">Tags</label>
                        <select name="tags" id="tags" multiple="multiple" class="custom-select">
                            {% for tag in tags %}
                            <option value="{{ tag.name }}" {{ 'selected' if tag.name in zone.tags }}>{{ tag.name }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn btn-primary btn-block">save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg"></div>
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $('#tags').select2({
            tags: true,
            tokenSeparators: [',', ' ']
        });
    });
</script>
{% endblock %}